layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;

	var table_filter = 'offer_list',
		form_filter = 'offer_search_form';
	var where_json = {
		_o: 'offer_id',
		_b: 'desc'
	};
	// 渲染表格
	table.render({
		elem: '#' + table_filter,
		id: table_filter,
		url: offer_list_url,
		method: 'post',
		height: 'full-200',
		where: {
			_o: 'offer_id',
			_b: 'desc'
		},
		cols: [offer_cols],
		page: {
			layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
			limit: 30,
			theme: '#393d49',
			groups: 8
		},
		autoSort: false,
		toolbar: false,
		defaultToolbar: [],
		initSort: {
			field: 'offer_id',
			type: 'desc'
		}
	});
	
	// 监听表头按钮
	table.on('toolbar(' + table_filter + ')', function(obj) {
		switch (obj.event) {
			case 'delOffer':
				var checkStatus = table.checkStatus(table_filter);
				var tr = checkStatus.tr;
				if (checkStatus.data.length == 0) {
					layer.msg('请选择要删除的数据', {
						icon: 2,
						anim: 6
					});
					return false;
				}
				var offer_id_arr = new Array();
				layer.confirm('确定删除选中数据吗？', function(index) {
					checkStatus.data.map(function(obj, i) {
						offer_id_arr.push(obj.offer_id);
					});
					$.post(offer_delete_url, {
						offer_id: offer_id_arr
					}, function(data) {
						if (data.code == 0) {
							tr.map(function(obj, i) {
								$(obj.selector).remove();
							})
							layer.msg('删除成功', {
								icon: 1
							});
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					}, 'json')
				})
				break;
			default:
				break;
		}
		return false;
	})
	form.render();

	// 表单input 查询
	$('form[lay-filter="' + form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if ($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		var where = {};
		where[name] = value;
		table.reload(table_filter, {
			where: where,
			page: {
				curr: 1
			}
		});
	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	})
	form.on('radio', function(data) {
		var value = data.value;
		var e = data.elem;
		var name = $(e).attr('name');
		select_search_list(name, value);
	});
	var select_search_list = function(field, value) {
		console.log(field, value);
		var where = {};
		where[field] = value;
		where_json[field] = value;
		table.reload(table_filter, {
			where: where,
			page: {
				curr: 1
			}
		});
	};
	var create_time_start = laydate.render({
		elem: 'input[name="offer_form_create_time_start"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				create_time_end.config.min = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search_list('create_time_start', value);
		}
	});
	var create_time_end = laydate.render({
		elem: 'input[name="offer_form_create_time_end"]',
		max: 0,
		theme: '#393D49',
		done: function(value, date, endDate) {
			if (value) {
				create_time_start.config.max = {
					year: date.year,
					month: date.month - 1,
					date: date.date
				};
			}
			select_search_list('create_time_end', value);
		}
	});
	// 表格工具监听
	table.on('tool(' + table_filter + ')', function(obj) {
		var data = obj.data;
		var e = obj.event;
		var tr = obj.tr;
		var offer_id = data.offer_id;
		switch (e) {
			case 'viewOffer':
				bjd_data_arr = new Array();
				bjd_offer_id = offer_id;
				data.param_arr.forEach(function(e1, i) {
					let temp = new Array();
					temp['goods_id'] = e1.goods_id;
					temp['goods_name'] = e1.goods_name;
					temp['goods_sku'] = e1.goods_sku;
					temp['sale_price'] = e1.sale_price;
					temp['api_price'] = e1.api_price;
					temp['is_lock'] = 1;
					temp['sale_state'] = 1;
					temp['audit_state'] = 1;
					temp['view_stock'] = e1.view_stock;
					bjd_data_arr.push(temp);
				})
				if(data.pingzheng){
					$('#pingzheng').val(data.pingzheng);
					$('#baojiadan_pingzheng').html('<span class="invoice-link"><a href="' + data.pingzheng_info.save_path +
				'" style="color: #ffffff;" target="_blank"><i class="layui-icon layui-icon-link"></i>查看凭证</a></span>');
				}  else {
					$('#baojiadan_pingzheng').html('');
					$('#pingzheng').val('');
				}
				create_baojiadan_html();
				$('#create_bjd_select_goods').show();
				create_is_out = true;
				break;
			case 'del':
				// 删除
				layer.confirm('确定删除吗？', function(index) {
					$.post(offer_delete_url, {
						offer_id: offer_id
					}, function(data) {
						if (data.code == 0) {
							obj.del();
							layer.msg('删除成功', {
								icon: 1
							});
							layer.close();
						} else {
							layer.msg(data.msg, {
								icon: 2,
								anim: 6
							});
						}
					}, 'json')
				})
				break;
			default:
				break;
		}
	})
})
